var tp_arr = new Array(
    "/static/app/transverse/system/auth07/ct/index.htm",
    "/static/app/transverse/plugins/poup_confirm_del.htm"
);
var keywordStr = window.getUrlParam("k");
var startDate = window.getUrlParam("sd");
var endDate = window.getUrlParam("nd");
var bc = window.getUrlParam("bc");
var pageNo = window.getUrlParam("p");
var totle = window.getUrlParam("t");
window.current = new Vue({
    el: '#current',
    template: window.getTemplate(tp_arr),
    data: {
        pages: [],
        totle: totle ? totle : 0,
        page: pageNo ? parseInt(pageNo) : 1,
        toPage: pageNo ? parseInt(pageNo) : 1,
        size: 10,
        keyword: keywordStr ? keywordStr : '',
        bc: bc ? bc : '0',
        startDate: startDate ? startDate : window.getDateTimeNow(),
        endDate: endDate ? endDate :window.getDateTimeNow(1),
        snList: [],
        snInfo: '',
        deLock: true
    },
    // 属性监听器
    watch: {
        /*if (newVal != '') {
            if(window.current['deLock']) {
                window.current['deLock'] = false;
                setTimeout(function () {
                    window.get_sn_list();
                },1000);
            }
        }*/
    },
    filters: {
        _formatDate: function (val) {
            return window.dateTimeFormat(val, "yyyy-MM-dd h:m:s");
        },
        _getRowsColor: function (index) {
            var className = 'even';
            if(index % 2 == 0){
                className = '';
            }
            return className;
        },
        _formatNumber: function (val) {
            val = parseInt(val);
            if(isNaN(val)){
                val = 0;
            }
            return val;
        },
        _currPage: function (val) {
            var page = window.current['page'];
            var pages = window.current['pages'].length;
            val = parseInt(val);
            var className = [];
            if(pages > 9 && page <= 5 && val > 9){
                className.push('display-none');
            }else if(pages > 9 && (pages - page < 5)){
                if(pages - val >= 9) {
                    className.push('display-none');
                }
            }else if(pages > 9 && page > 5 && (val < page - 4 || val > page + 4)){
                className.push('display-none');
            }
            /*if(pages > 9 && page > 5 && ((page - 4 > val) || (page + 4 < val))){
                className.push('display-none');
            }*/
            if(page == val){
                className.push('active');
            }
            return className;
        },
        _currPageLeft: function () {
            var page = window.current['page'];
            var pagesArr = window.current['pages'];
            var pages = pagesArr?pagesArr.length:0;
            var className = [];
            if(!(pages > 9 && page > 5)){
                className.push('display-none');
            }
            return className;
        },
        _currPageRight: function () {
            var page = window.current['page'];
            var pagesArr = window.current['pages'];
            var pages = pagesArr?pagesArr.length:0;
            var className = [];
            if(pages <= 9 || (pages - page < 5)){
                className.push('display-none');
            }
            return className;
        },
        _fIfFinishType: function (val) {
            var name;
            switch (val) {
                case "0":
                    name = '未完成';
                    break;
                case "1":
                    name = '已完成';
                    break;
                default:
                    name = '未知';
                    break;
            }
            return name;
        },
        _fCFlagType: function (val) {
            var name;
            switch (val) {
                case "0":
                    name = '未审核';
                    break;
                case "1":
                    name = '已审核';
                    break;
                case "2":
                    name = '反审核';
                    break;
                default:
                    name = '未知';
                    break;
            }
            return name;
        },
        _fIfCloseType: function (val) {
            var name;
            switch (val) {
                case "0":
                    name = '未结案';
                    break;
                case "1":
                    name = '自动结案';
                    break;
                case "2":
                    name = '反结案';
                    break;
                case "3":
                    name = '手工结案';
                    break;
                default:
                    name = '未知';
                    break;
            }
            return name;
        }
    },
    computed: {},
    methods: {
        onSearch: function () {
            window.get_sn_list_to_page_one();
        },
        onClear: function () {
            this['keyword'] = '';
            window.get_sn_list_to_page_one();
        },
        onDel: function (index) {
            window.del_sn_to_confirm(index);
        },
        onDelSn: function () {
            window.del_sn_info();
        },
        onSetPage: function (val) {
            var page = this['page'];
            var totle = this['totle'];
            var keyword = this['keyword'];
            var startDate = this['startDate'];
            var endDate = this['endDate'];
            if(page != val){
                var hrefStr =  window.location.pathname + '?a=1';
                if((keywordStr == null && keyword == '')|| keywordStr == keyword){
                    hrefStr += '&p=' + val + '&t=' + totle;
                }
                if(keyword != ''){
                    hrefStr += '&k=' + keyword;
                }
                if(startDate != ''){
                    hrefStr += '&sd=' + startDate;
                }
                if(endDate != ''){
                    hrefStr += '&nd=' + endDate;
                }
                if(cw != ''){
                    hrefStr += '&cw=' + cw;
                }
                if(ct != ''){
                    hrefStr += '&ct=' + ct;
                }
                if(wg != ''){
                    hrefStr += '&wg=' + wg;
                }
                window.location.href = hrefStr;
            }
        },
        onToPage: function () {
            var page = this['page'];
            var to_page = this['toPage'];
            var totle = this['totle'];
            var keyword = this['keyword'];
            var startDate = this['startDate'];
            var endDate = this['endDate'];
            var pagesArr = this['pages'];
            var cw = this['cw'];
            var ct = this['ct'];
            var wg = this['wg'];
            var pages = pagesArr?pagesArr.length:0;
            if(to_page > pages){
                window.custom_toast('无法跳转该页面！');return;
            }
            if(page != to_page){
                var hrefStr =  window.location.pathname + '?a=1';
                if((keywordStr == null && keyword == '')|| keywordStr == keyword){
                    hrefStr += '&p=' + to_page + '&t=' + totle;
                }
                if(keyword != ''){
                    hrefStr += '&k=' + keyword;
                }
                if(startDate != ''){
                    hrefStr += '&sd=' + startDate;
                }
                if(endDate != ''){
                    hrefStr += '&nd=' + endDate;
                }
                if(cw != ''){
                    hrefStr += '&cw=' + cw;
                }
                if(ct != ''){
                    hrefStr += '&ct=' + ct;
                }
                if(wg != ''){
                    hrefStr += '&wg=' + wg;
                }
                window.location.href = hrefStr;
            }
        },
        onMoreSnList: function () {
            var page = this['page'];
            var size = this['size'];
            var totle = this['totle'];
            if(page * size < totle){
                window.get_sn_list_more();
            }
        },
        onRefresh: function () {
            //_currPageRightwindow.location.reload();
            window.get_mo_list();
        },
        onRadioChecked: function (key, val) {
            if(this[key] != val){
                this[key] = val;
                window.get_sn_list_to_page_one();
            }
        }
    }
});

window.get_sn_list_to_page_one = function () {
    window.current['page'] = 1;
    window.current['snList'] = [];
    window.current['totle'] = 0;
    window.get_sn_list();
}

window.get_sn_list_more = function () {
   var page = window.current['page'];
    window.current['page'] = parseInt(page) + 1;
    window.get_sn_list();
}

window.get_sn_list = function () {
    var keyword = window.current['keyword'];
    var startDate = window.current['startDate'];
    var endDate = window.current['endDate'];
    var bc = window.current['bc'];
    var page = window.current['page'];
    var size = window.current['size'];
    if(keyword != ''){
        window.current['page'] = 1;
        window.current['totle'] = 0;
        page = 1;
    }
    var data_arr = {
        keyword : keyword,
        fStartDate : startDate,
        fEndDate : endDate,
        bc : bc,
        page: page,
        size: size
    };
    window.ajax({
        url: "/service/Goods/getSnListCt",
        data: data_arr,
        async: false,
        success: function (d) {
            if(keyword != ''){
                window.current['keyword'] = '';
                window.current['snList'] = [];
            }
            if (d['status'] == 1) {
                var totle = window.current['totle'];
                var snList = window.current['snList'];
                var list = d['data'];
                if (list) {
                    $.each(list, function(i, info) {
                        snList.push(info);
                    });
                    window.current['snList'] = snList;
                    if (totle == 0) {
                        window.get_sn_list_count();
                    }
                }
            } else {
                var msg = '';
                switch (d['code']) {
                    case '3307':
                        msg = keyword + '这个编码无法识别型号';
                        break;
                    default:
                        msg = d['msg'];
                        break;
                }
                window.custom_toast(msg);
            }
        }
    });
}

window.get_sn_list_count = function () {
    var keyword = window.current['keyword'];
    var startDate = window.current['startDate'];
    var endDate = window.current['endDate'];
    var bc = window.current['bc'];
    var data_arr = {
        keyword : keyword,
        fStartDate : startDate,
        fEndDate : endDate,
        bc : bc
    };
    window.ajax({
        url: "/service/Goods/getSnListCtCount",
        data: data_arr,
        async: false,
        success: function (d) {
            if (d['status'] == 1) {
                var totle = parseInt(d['data']);
                window.current['totle'] = totle;
            } else {
                window.custom_toast(d['msg']);
            }
        }
    });
}

window.del_sn_to_confirm = function (index) {
    window.current['snInfo'] = window.current['snList'][index];
    window.poup('#confirm_del');
}

window.del_sn_info = function () {
    var snInfo = window.current['snInfo'];
    var data_arr = {
        barcode_ct_uuid : snInfo['barcode_ct_uuid']
    };
    window.ajax({
        url: "/service/Goods/delSnInfoCt",
        data: data_arr,
        async: false,
        success: function (d) {
            if (d['status'] == 1) {
                window.poup_close('#confirm_del');
                window.custom_toast('删除成功！');
                window.get_sn_list_to_page_one();
            } else {
                window.custom_toast(d['msg']);
            }
        }
    });
}

window.setPages = function (count) {
    var size = window.current['size'];
    var pageCount = Math.ceil(count / size);
    var pages = [];
    for(let i = 1; i　<= pageCount; i++) {
        pages.push(i);
    }
    window.current['pages'] = pages;
}

window.get_sn_list();

//日期范围限制
var start = {
    elem: '#start',
    format: 'YYYY-MM-DD hh:mm:ss',
    istime: true, //是否开启时间选择
    isclear: false, //是否显示清空
    istoday: false, //是否显示今天
    choose: function (datas) {
        window.current['startDate'] = datas;
        window.get_sn_list_to_page_one();
    }
};

var end = {
    elem: '#end',
    format: 'YYYY-MM-DD hh:mm:ss',
    istime: true, //是否开启时间选择
    isclear: false, //是否显示清空
    istoday: false, //是否显示今天
    choose: function (datas) {
        window.current['endDate'] = datas;
        window.get_sn_list_to_page_one();
    }
};
laydate(start);
laydate(end);
